package com.open.capacity.reimburse.service.impl;

import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.model.system.SysUser;
import com.open.capacity.reimburse.dao.HtAgreementAuthorizeDao;
import com.open.capacity.reimburse.dao.HtAgreementPayDao;
import com.open.capacity.reimburse.entity.*;
import com.open.capacity.reimburse.exception.BusinessException;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.user.model.SysFile;
import com.open.capacity.user.service.SysFileService;
import com.open.capacity.utils.SysUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.dao.HtAgreementDao;
import com.open.capacity.reimburse.service.HtAgreementService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;


@Service
public class HtAgreementServiceImpl implements HtAgreementService {

    @Autowired
    private HtAgreementDao htAgreementDao;

    @Autowired
    private HtAgreementAuthorizeDao htAgreementAuthorizeDao;

    @Autowired
    private HtAgreementPayDao htAgreementPayDao;

    /**
     * 添加
     *
     * @param htAgreement
     */
    @Transactional(rollbackFor = Exception.class)
    public int save(HtAgreement htAgreement) {
        validition(htAgreement);

        SysUser loginUser = SysUserUtil.getLoginAppUser();
        htAgreement.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        htAgreement.setStatus(1);
        htAgreement.setCreateUserid(loginUser.getId());
        htAgreement.setCreateUsername(loginUser.getRealName());
        htAgreement.setCreateTime(new Date());
        htAgreement.setUpdateTime(new Date());

        for (HtAgreementPay item : htAgreement.getPayList()) {
            item.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            item.setBillId(htAgreement.getId());
            item.setUpdateTime(new Date());
            item.setUpdateUserid(loginUser.getId());
            item.setUpdateUsername(loginUser.getRealName());
            htAgreementPayDao.save(item);
        }

        return htAgreementDao.save(htAgreement);
    }

    private void validition(HtAgreement htAgreement) {
        HtAgreement exists = findByNumber(htAgreement.getNumber());
        if (exists != null && (htAgreement.getId() == null || !htAgreement.getId().equals(exists.getId()))) {
            Assert.isTrue(false, "合同编号冲突");
        }
    }

    /**
     * 修改
     *
     * @param htAgreement
     */
    @Transactional(rollbackFor = Exception.class)
    public int update(HtAgreement htAgreement) {
        validition(htAgreement);

        SysUser loginUser = SysUserUtil.getLoginAppUser();
        htAgreement.setUpdateTime(new Date());
        htAgreement.setUpdateUserid(loginUser.getId());
        htAgreement.setUpdateUsername(loginUser.getRealName());

        htAgreementPayDao.deleteByBillid(htAgreement.getId());
        for (HtAgreementPay item : htAgreement.getPayList()) {
            item.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            item.setBillId(htAgreement.getId());
            item.setUpdateTime(new Date());
            item.setUpdateUserid(loginUser.getId());
            item.setUpdateUsername(loginUser.getRealName());
            htAgreementPayDao.save(item);
        }
        return htAgreementDao.update(htAgreement);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return htAgreementDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public HtAgreement findById(String id) {
        HtAgreement model = htAgreementDao.findById(id);
        model.setPayList(htAgreementPayDao.findByBillid(id));
        return model;
    }

    @Override
    public HtAgreement findByNumber(String number) {
        return htAgreementDao.findByNumber(number);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<HtAgreement> pageAll(Map<String, Object> params) {
        SysUser loginUser = SysUserUtil.getLoginAppUser();
        params.put("createUserid", loginUser.getId());
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<HtAgreement> list = htAgreementDao.findAll(params);
        PageInfo<HtAgreement> pageInfo = new PageInfo(list);

        return PageResult.<HtAgreement>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<HtAgreement> findAll(Map<String, Object> params) {
        return htAgreementDao.findAll(params);
    }

    @Override
    public int share(String id, List<HtAgreementAuthorize> agreementAuthorize, Integer isShare) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();

        HtAgreement fileinfo = htAgreementDao.findById(id);
        Assert.isTrue(loginUser.getId().equals(fileinfo.getCreateUserid()), "您无权分享该文件");

        htAgreementAuthorizeDao.deleteByAgreementId(id);
        for (HtAgreementAuthorize authorize : agreementAuthorize) {
            authorize.setAgreementId(id);
            if (authorize.getAuthType() == null) {
                authorize.setAuthType("1,3");
            }
            htAgreementAuthorizeDao.save(authorize);
        }
        int count = 0;
        //count = htAgreementDao.update(fileinfo);
        return count;
    }

}
